package app.itgungnir.compose.wan.business.splash

import android.content.res.Configuration
import androidx.annotation.RawRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.itgungnir.compose.wan.R
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import app.itgungnir.compose.wan.widgets.WanHeader
import app.itgungnir.compose.wan.widgets.WanInfo
import app.itgungnir.compose.wan.widgets.WanTitle
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsHeight

@Composable
fun SplashUI(hasLaunched: Boolean = false, onAnimFinish: () -> Unit) {
    Scaffold {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(bottom = 35.dp)
                .navigationBarsPadding()
                .background(MaterialTheme.colors.background),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Spacer(modifier = Modifier.statusBarsHeight())
            if (hasLaunched) {
                Image(
                    modifier = Modifier.weight(1F),
                    painter = painterResource(id = R.mipmap.ic_launcher_round),
                    contentDescription = "",
                    alignment = Alignment.BottomCenter
                )
            } else {
                LottieAnimationView(modifier = Modifier.weight(1F), rawResource = R.raw.lottie_splash) {
                    onAnimFinish.invoke()
                }
            }
            Spacer(modifier = Modifier.height(10.dp))
            WanHeader(content = "玩安卓")
            Spacer(modifier = Modifier.height(5.dp))
            WanTitle(content = "Powered by Jetpack Compose")
            Spacer(modifier = Modifier.height(5.dp))
            WanInfo(content = "Developed by ITGungnir")
        }
    }
}

@Composable
private fun LottieAnimationView(modifier: Modifier = Modifier, @RawRes rawResource: Int, onAnimFinish: () -> Unit) {
    val composition by rememberLottieComposition(spec = LottieCompositionSpec.RawRes(rawResource))
    val progress by animateLottieCompositionAsState(composition = composition)
    LottieAnimation(modifier = modifier, composition = composition, progress = progress)
    LaunchedEffect(key1 = progress) {
        if (progress == 1.0F) {
            onAnimFinish.invoke()
        }
    }
}

@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun SplashPreview() {
    WanComposePreviewTheme {
        SplashUI {}
    }
}
